﻿using AntdUI;
using Lima.ResumeSys.Class;
using Lima.ResumeSys.DAL;
using Lima.ResumeSys.Models;
using Lima.ResumeSys.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using static Lima.ResumeSys.Models.Organization;
using static Lima.ResumeSys.Models.RewardPunish;

namespace Lima.ResumeSys.Views
{
    public partial class OrganizationControl : UserControl
    {
        private AntdUI.Window window;
        List<Organization> antList;
        PerSonInfoDAL dal = new PerSonInfoDAL();
        private clsDbOperate dbOperate = new clsDbOperate();
        OrganizationDAL orgdal = new OrganizationDAL();
        private string CurrentOrgId = string.Empty;
        public OrganizationControl(AntdUI.Window _window)
        {
            window = _window;
            InitializeComponent();
            InitTableColumns();
            //InitData("");
            BindTreeViewData();
        }
        #region table
        private void InitTableColumns()
        {
            orgtable.Columns = new ColumnCollection() {
                new Column("","序号"){
                    Width = "50",
                    Render = (value,record,rowindex)=>{return (rowindex+1); },
                    Fixed = true,//冻结列
                },
                new Column("Code", "编码", ColumnAlign.Center),
                new Column("OrgName", "名称",ColumnAlign.Center),
                 new Column("Sort", "排序",ColumnAlign.Center),
                new Column("CellLinks", "操作", ColumnAlign.Center),
            };
        }

        private void InitData(string pParCode)
        {
            if (string.IsNullOrEmpty(this.CurrentOrgId))
            {
                MessageBox.Show("请选择组织节点!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            antList = new List<Organization>();

            QueryOrganizationModel query = new QueryOrganizationModel();
            query.OrgName = txtname.Text;
            query.ParCode = CurrentOrgId;
            var list = orgdal.GetList(query);
            foreach (var item in list)
            {
                antList.Add(new Organization
                {
            
                    Code = item.Code,
                    OrgName = item.OrgName,
                    Sort = item.Sort,
                    StrUpDatetime = item.StrUpDatetime,
                    CellLinks = new CellLink[] {
                    new CellButton(item.ID,"编辑",TTypeMini.Primary),
                    new CellButton(item.ID,"删除",TTypeMini.Error)},
                });
            }
            orgtable.DataSource = antList;
        }
        #endregion

        private void BindTreeViewData()
        {
            var dataTable = orgdal.GetTreeViewData();

            var allData = dataTable.AsEnumerable().Select(row => new Organization
            {
                ID = row["Id"].ToString(),
                Code = row["Code"].ToString(),
                OrgName = row["OrgName"].ToString(),
                ParCode = row["ParCode"].ToString(),
            }).ToList();

            TreeBuilder treeBuilder = new TreeBuilder();

            treeBuilder.BindTreeViewWithParentId(orgTree, allData, CurrentUser.UserOrgId);

            orgTree.ExpandAll();
        }

        /// <summary>
        /// 点击节点显示对应的子节点数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void orgTree_NodeMouseClick(object sender, TreeSelectEventArgs e)
        {
            CurrentOrgId = e.Item.ID.ToString();
            InitData(CurrentOrgId);
        }

        private void orgtable_CellButtonClick(object sender, TableButtonEventArgs e)
        {

            switch (e.Btn.Text)
            {
                //暂不支持进入整行编辑，只支持指定单元格编辑，推荐使用弹窗或抽屉编辑整行数据
                case "编辑":
                    OrgForm form = new OrgForm(e.Btn.Id, CurrentOrgId);
                    
                    form.ShowDialog();
                    if (form.DialogResult == DialogResult.OK)
                    {
                        InitData(CurrentOrgId);
                    }
                    break;
                case "删除":
                    var result = Modal.open(window, "删除警告！", "确认要删除选择的数据吗？", TType.Warn);
                    if (result == DialogResult.OK)
                    {
                        if (orgdal.Del(e.Btn.Id))
                        {
                            AntdUI.Message.success(window, "删除成功！", autoClose: 3);
                            InitData(CurrentOrgId);
                        }
                        else
                        {
                            AntdUI.Message.error(window, "删除失败！", autoClose: 3);
                        }
                    }
                    break;
            }
        }

        private void btnSeach_Click(object sender, EventArgs e)
        {
            InitData(CurrentOrgId);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.CurrentOrgId))
            {
                MessageBox.Show("请选择组织节点!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            OrgForm form = new OrgForm("", CurrentOrgId);
            form.ShowDialog();
            if (form.DialogResult == DialogResult.OK)
            {
                InitData(CurrentOrgId);
            }
        }
    }
}
